package com.atjinggu.jinggumail.product.service.impl;

import com.atjinggu.jinggumail.product.entity.CategoryBrandRelationEntity;
import com.atjinggu.jinggumail.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atjinggu.jinggumail.common.utils.PageUtils;
import com.atjinggu.jinggumail.common.utils.Query;

import com.atjinggu.jinggumail.product.dao.BrandDao;
import com.atjinggu.jinggumail.product.entity.BrandEntity;
import com.atjinggu.jinggumail.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), new QueryWrapper<BrandEntity>());

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wr = new QueryWrapper();
        if (StringUtils.isNotBlank(key)) {
            wr.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), wr);
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //修改自身表
        this.updateById(brand);
        if(StringUtils.isNotBlank(brand.getName())) {
            //修改品牌与分类关联关系表
            CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
            entity.setBrandName(brand.getName());
            UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper();
            wrapper.eq("brand_id",brand.getBrandId());
            categoryBrandRelationService.update(entity,wrapper);
        }
    }

}